package geektime.concurrent.part2.race;

/**
 * 单线程操作的共享数据类
 */
public class SingleThreadShareData extends ShareData {

    // 是否完成随机数生成的动作
    private static final String IS_GEN_NUMBERS_DONE = "IS_GEN_NUMBERS_DONE";

    // 获取生成随机数状态信息
    public boolean getConcurrentSignal() {
        if (concurrentSignalMap != null && concurrentSignalMap.get(IS_GEN_NUMBERS_DONE) != null) {
            return (boolean)concurrentSignalMap.get(IS_GEN_NUMBERS_DONE);
        }
        return false;
    }

    // 设置生成随机数状态信息
    public void setConcurrentSignal(boolean signal) {
        Boolean oldStatus = (Boolean)concurrentSignalMap.get(IS_GEN_NUMBERS_DONE);
        if (oldStatus == null) {
            concurrentSignalMap.put(IS_GEN_NUMBERS_DONE, signal);
        } else {
            concurrentSignalMap.replace(IS_GEN_NUMBERS_DONE, signal);
        }
    }
}
